package com.example.java.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.java.Entity.Admin;
import com.example.java.Entity.User;
import com.example.java.Mapper.AdminMapper;
import com.example.java.Mapper.LoginMapper;
import com.example.java.Mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private AdminMapper adminMapper;

    /*
    登录
    如果用户名为空或者密码不正确:则登录失败
    */
    public int login(Admin admin){
        log.info("service.login传入参数:{}", JSON.toJSONString(admin));
        if (Objects.isNull(adminMapper.selectOne(new LambdaQueryWrapper<Admin>()
                .eq(Admin::getUsername,admin.getUsername())
                .eq(Admin::getPassword,admin.getPassword())))){
            return 0;
        }
        return 1;
    }
    //注册
    public int addUser(Admin admin){
        if (!Objects.isNull(adminMapper.selectOne(new LambdaQueryWrapper<Admin>().eq(Admin::getUsername,admin.getUsername())))){
            return 0;
        }
        return adminMapper.insert(admin);
    }

    //忘记密码
    public int updateUser(Admin admin){
        if (Objects.isNull(adminMapper.selectOne(new LambdaQueryWrapper<Admin>().eq(Admin::getUsername,admin.getUsername())))) return 0;
        return adminMapper.updateById(admin);

    }
//
//    public Map<String,Object> login(User user){
//        User u = userMapper.selectOne(new LambdaQueryWrapper<User>()
//                .eq(User::getUsername, user.getUsername()));
//        if (Objects.isNull(u)) throw new RuntimeException("用户不存在");
//        if (!u.getPassword().equals(user.getPassword())) throw new RuntimeException("密码错误");
//        /**
//         * TODO:密码校验
//         * */
//
//        Integer id = u.getId();
//        Role role =  RoleMapper.getRoleById(id);
//        HashMap<String, Object> result = new HashMap<>();
//        result.put("role",role);
//        /**
//         * js:  if (res.data.role === "admin"){
//         *     router.push("/admin")
//         * }else{
//         *     router.push("/index")
//         * }
//         *
//         * */
//    }

}
